package com.zz.bid.web.controller;

import com.zz.bid.entity.Menu;
import com.zz.bid.exception.base.BidException;
import com.zz.bid.service.GroupService;
import com.zz.bid.service.MenuService;
import com.zz.bid.service.UserService;
import com.zz.bid.to.*;
import com.zz.bid.web.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private MenuService menuService;

    @PostMapping("/login")
    public ResponseWrapper login(@RequestBody LoginTO loginTO) {
        try {
            UserTO userTO = userService.authenticate(loginTO.getUserName(), loginTO.getPassword());
            return ResponseWrapper.builder().data(userTO).status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return ResponseWrapper.builder().message(e.getMessage()).status(HttpStatus.UNAUTHORIZED.value())
                    .path(request.getServletPath()).build();
        }
    }

    @PostMapping("/logout")
    public ResponseWrapper logout(@RequestHeader String token) {
        try {
            userService.logout(token);
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().message(e.getMessage()).status(HttpStatus.INTERNAL_SERVER_ERROR.value())
                    .path(request.getServletPath()).build();
        }
    }

    @GetMapping("/getUserInfo")
    public ResponseWrapper getUserInfo(@RequestHeader("token") String token) {
        try {
            UserTO userTO = userService.getUserInfo(token);
            return ResponseWrapper.builder().data(userTO).path(request.getServletPath()).data(userTO).status(HttpStatus.OK.value()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            return ResponseWrapper.builder().message(e.getMessage()).status(HttpStatus.UNAUTHORIZED.value())
                    .path(request.getServletPath()).build();
        }
    }

    @GetMapping("/getMenuAccess")
    public ResponseWrapper getMenuAccess() {
        try {
            List<Menu> menus = menuService.getMenus();
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).data(menus).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().path(request.getServletPath()).status(HttpStatus.INTERNAL_SERVER_ERROR.value())
                    .message(e.getMessage()).build();
        }
    }

    @GetMapping("/getGroups")
    public ResponseWrapper getGroups() {
        try {
            List<GroupTO> groups = groupService.getGroups();
            return ResponseWrapper.builder().path(request.getServletPath()).data(groups).status(HttpStatus.OK.value()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).path(request.getServletPath()).message(e.getMessage()).build();
        }
    }

    @GetMapping("/getPassports")
    public ResponseWrapper getUsers() {
        try {
            List<UserTO> users = userService.getUsers();
            return ResponseWrapper.builder().data(users).status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).
                    message(e.getMessage()).path(request.getContextPath()).build();
        }
    }

    @PostMapping("/setGroupAccess")
    public ResponseWrapper setGroupAccess(@RequestBody AddGroupAccessTO addGroupAccessTO) {
        try {
            groupService.setGroupAccess(addGroupAccessTO.getId(), addGroupAccessTO.getAddedAccess(), addGroupAccessTO.getRemovedAccess());
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).message(e.getMessage()).path(request.getServletPath()).build();
        }
    }

    @PostMapping("/addGroup")
    public ResponseWrapper addGroup(@RequestParam String groupName) {
        try {
            groupService.addGroup(groupName);
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).message(e.getMessage()).path(request.getServletPath()).build();
        }
    }

    @PostMapping("/addUser")
    public ResponseWrapper addUser(@RequestBody UserTO userTO) {
        try {
            userService.addUser(userTO.getAccountName(), userTO.getPassword(), userTO.getGroupId(), userTO.getBanned());
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).path(request.getServletPath()).message(e.getMessage()).build();
        }
    }


    @PostMapping("/changeUserPwd")
    public ResponseWrapper changeUserPwd(@RequestBody ChangePwdTO changePwdTO) {
        try {
            userService.updatePwd(changePwdTO.getAccount(), changePwdTO.getOldPwd(), changePwdTO.getNewPwd());
            return ResponseWrapper.builder().status(HttpStatus.OK.value()).path(request.getServletPath()).build();
        } catch (BidException e) {
            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            return ResponseWrapper.builder().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).path(request.getServletPath()).message(e.getMessage()).build();
        }
    }
}
